<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<jsp:useBean id="now" class="java.util.Date" scope="page"/>
<!DOCTYPE html>
<html lang="zh">
<html>
<head>
    <title>批量添加积分</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style type="text/css">
        .index-outside {
            min-height: auto !important;
        }

        .index-outside > section .section-main {
            min-width: auto !important;
        }
    </style>
</head>
<body>
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="editIntegralForm" class="layui-form mt20" method="post"
                      action="${ctx}/member/integral/userIntegral/batchAddIntegral.do">
                    <input type="hidden" name="activityId" value="${activityId}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">人员分类<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <select name="type" id="type" lay-verify="type" lay-filter="type">
                                    <c:forEach var="type" items="${mtypes}">
                                        <option value="${type.key}">${type.value}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <c:if test="${packageCount.isOpen == 1}">
                                <div class="layui-form-mid layui-word-aux">当前短信剩余<span
                                        class="red">${packageCount.totalNum - packageCount.useNum}</span>条
                                </div>
                                <div class="layui-form-mid layui-word-aux"><a href="javascript:forwardCharge();"
                                                                              style="color: #1E9FFF">充值</a>
                                </div>
                            </c:if>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select name="integralTypeId" lay-filter="types" id="types">
                                    <option value="">请选择</option>
                                    <c:forEach var="type" items="${types}">
                                        <option value="${type.id}">${type.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">积分项</label>
                            <div class="layui-input-inline">
                                <select name="integralItemId" lay-filter="items" id="items">
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">分数 :</label>
                            <div class="layui-input-inline" style=" margin-top: 3px;">
                                <span class="active-red" id="itemFractionVal" style="font-size: 20px"></span>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否发送短信<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <div class="layui-input-inline">
                                <input type="radio" name="isSendMessages" lay-filter="isSendMessages" value="1"
                                       title="是" checked="checked">
                                <input type="radio" name="isSendMessages" lay-filter="isSendMessages" value="0"
                                       title="否">
                            </div>
                        </div>
                    </div>
                    <div id="isSendMessagesDiv">
                        <fieldset class="border mb10">
                            <legend>发送短信</legend>
                            <div class="form-inner">
                                <div class="layui-form-item" style="width: 420px">
                                    <label class="layui-form-label">发送时间<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-block">
                                        <input class="layui-input" type="text" name="sendTimeString"
                                               value="<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" />"
                                               placeholder="发送时间">
                                        <div class="layui-form-mid layui-word-aux"> 发送时间小于或等于当前时间,则会立即发送</div>
                                    </div>
                                </div>
                                <div class="layui-form-item" style="width: 1100px">
                                    <label class="layui-form-label">模板配置<span class="f-verify-red">*</span></label>
                                    <div class="layui-input-block">
                      <textarea name="template" placeholder="模板配置" lay-verify="template" class="layui-textarea"
                                style="width: 85%;height: 70px"
                                maxlength="200" onchange="this.value=this.value.substring(0, 300)"
                                onkeydown="this.value=this.value.substring(0, 300)"
                                onkeyup="this.value=this.value.substring(0, 300)"></textarea>
                                        <div class="layui-form-mid layui-word-aux">最多200字符</div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">说明：</label>
                                        <div class="layui-input-block" style="margin-top: 10px;">
                                            {user} 表示活动参与者, {title} 表示活动名称, {typeName} 表示活动类型{fraction} 表示获得积分
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">模板：</label>
                                        <div class="layui-input-block" style="margin-top: 10px;">
                                            恭喜您，{user}您参加的{title}活动，获得了{fraction}{typeName}积分。
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">内容：</label>
                                        <div class="layui-input-block" style="margin-top: 10px;">
                                            恭喜您，雷布斯您参加的徒步挑战赛活动，获得了20.0线下徒步积分。
                                        </div>
                                    </div>
                                </div>
                                <div class="cl"></div>
                            </div>
                        </fieldset>
                    </div>

                </form>
            </div>
        </div>
    </section>
</div>

<script type="text/javascript">
    var laytpl = null;
    var laypage = null;
    var element = null;
    var form = null;
    var isSendMessages_val = 1;
    $(function () {
        layui.use(['laytpl', 'laypage', 'element', 'form', 'laydate'], function () {
            laytpl = layui.laytpl;
            laypage = layui.laypage;
            element = layui.element;
            form = layui.form;
            var laydate = layui.laydate;


            //是否发送短信
            form.on('radio(isSendMessages)', function (data) {
                var isisSendMessagesr_val = data.value;
                if (isisSendMessagesr_val == 0) {
                    isSendMessages_val = 0
                    $("#isSendMessagesDiv").hide();
                } else {
                    isSendMessages_val = 1;
                    $("#isSendMessagesDiv").show();
                }
                form.render();
            });
            // 积分类型
            form.on('select(types)', function (data) {
                var typeId = data.value
                $("#items").html("");
                $("#itemFractionVal").html("")
                loadIntegralItemssData(typeId, "");
            });
            // 积分类型
            form.on('select(items)', function (data) {
                $("#itemFractionVal").html("")
                var itemFractionVal = $(data.elem).find("option:selected").attr("fractionVal");
                $("#itemFractionVal").html(itemFractionVal);
            });

            var sendTime = {
                elem: 'input[name=sendTimeString]',
                type: 'datetime'
            };
            laydate.render(sendTime);
        });
    })

    function submitQuartz() {
        var result = true;

        if (isSendMessages_val == 1) {
            if (!checkPersonNum()) {
                top.layer.msg('请选择人数不为零的发送对象', {icon: 2});
                return false;
            }

            var template = $('textarea[name=template]').val();
            if (!template || !template.trim()) {
                util.layerMsgError("请填写模板配置")
                result = false;
                return result;
            }
            var sendTimeString = $('input[name=sendTimeString]').val();
            if (!sendTimeString) {
                util.layerMsgError("请设置发送时间")
                result = false;
                return result;
            }
            if ($('#behavior').val() == 'modifyTime') {
                var now = new Date();
                if (now.getTime() > new Date(sendTimeString).getTime()) {
                    util.layerMsgError("发送时间必须大于当前时间!")
                    result = false;
                    return result;
                }
            }
        }
        $.ajax({
            url: '${ctx}/member/integral/userIntegral/batchAddIntegral.do',
            type: 'POST',
            async: false,
            data: $('#editIntegralForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("添加成功")
                } else {
                    util.layerMsgError(res.description)
                    result = false;
                }
            }
        });
        return result;
    }

    function forwardCharge() {
        parent.location.href = "${ctx}/charge/package/smsPackageList.do";
    }

    function checkPersonNum() {
        var types = $("#types").val();
        var index = $('#type option:selected').text();
        /*        alert(index)
                var typeStr = findValue(index, types);
                alert(typeStr)
                var reg = /([^\d]+)(\d+)([^\d]+)/;
                var r = typeStr.match(reg);
                if (!r) {
                    return false;
                }
                var num = typeStr.replace(reg, '$2');
                alert(num)*/

        var num = parseInt(index)

        if (num == 0) {
            return false;
        } else {
            return true;
        }
    }

    function findValue(key, str) {
        var index = str.indexOf(key + "=");
        var key_new = key + "=";
        var sub = str.substring(index + key_new.length);

        var key_last = '';
        if (sub.indexOf(",") > -1) {
            key_last = ",";
        } else {
            key_last = "}";
        }
        sub = sub.substring(0, sub.indexOf(key_last));
        return sub;
    }

    function loadIntegralItemssData(typeId, itemId) {
        $.post("${ctx}/member/integral/integralItem/listAll.do", {
            "typeId": typeId,
            "partnerId": '${partnerId}'
        }, function (data) {
            var items = new Array();
            items.push("<option value = ''>" + "选择积分项" + "</option>");
            for (var i = 0; i < data.length; i++) {
                if (itemId == data[i].id) {
                    items.push("<option value = '" + data[i].id + "' fractionVal =  '" + data[i].itemFraction + "'selected='selected'>" + data[i].name + "</option>");
                } else {
                    items.push("<option value = '" + data[i].id + "' fractionVal =  '" + data[i].itemFraction + "'>" + data[i].name + "</option>");
                }
            }
            $("#items").append(items.join(""));
            form.render('select');
        });
    }

</script>
</body>
</html>
